home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / content / historyResults.js < prev    next >
Text File  |  2009-10-07  |  17KB  |  473 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* historyResults.js
  3.  * Responsible for results view
  4.  */
  5.  
  6.  
  7. bthistory.controllers["results"] = {
  8.     handleUpArrow: function(){
  9.         var sels = document.getElementsByClassName("selected");
  10.         var box = document.getElementById("resultsdata");
  11.         if(!box){
  12.             return;
  13.         }
  14.         var body = document.getElementById("body");
  15.         if(sels.length == 0){
  16.             if(box.childNodes.length == 0){
  17.                 return;
  18.             } else {
  19.                 bthistory.selectNode(box.lastChild);
  20.                 bthistory.scrollIntoView(body, box.lastChild);
  21.             }
  22.         } else {
  23.             var sel = sels[0];
  24.             if(sel.previousSibling){
  25.                 bthistory.selectNode(sel.previousSibling);
  26.                 bthistory.scrollIntoView(body, sel.previousSibling);
  27.             } else {
  28.                 var nextarrow = document.getElementById("searchprev");
  29.                 if(nextarrow){
  30.                     nextarrow.click();
  31.                 }
  32.             }
  33.         }
  34.     },
  35.     handleDownArrow: function(){
  36.         var sels = document.getElementsByClassName("selected");
  37.         var box = document.getElementById("resultsdata");
  38.         if(!box){
  39.             return;
  40.         }
  41.         var body = document.getElementById("body");
  42.         if(sels.length == 0){
  43.             if(box.childNodes.length == 0){
  44.                 return;
  45.             } else {
  46.                 bthistory.selectNode(box.firstChild);
  47.                 bthistory.scrollIntoView(body, box.firstChild);
  48.             }
  49.         } else {
  50.             var sel = sels[0];
  51.             if(sel.nextSibling){
  52.                 bthistory.selectNode(sel.nextSibling);
  53.                 bthistory.scrollIntoView(body, sel.nextSibling);
  54.             } else {
  55.                 var nextarrow = document.getElementById("searchnext");
  56.                 if(nextarrow){
  57.                     nextarrow.click();
  58.                 }
  59.             }
  60.         }
  61.     },
  62.     onHistoryAdd: function(data) {
  63.     },
  64.     onHistoryChange: function(data) {
  65.     },
  66.     handleResize: function(){
  67.         var winx = document.getElementById("boomtangoHistory");
  68.         var win = { 
  69.             h: winx.boxObject.height,
  70.             w: winx.boxObject.width,
  71.             x: 0,
  72.             y: 0
  73.         };
  74.         var panel = document.getElementById("rightpanel");
  75.         var panelbox = { 
  76.             h: panel.boxObject.height,
  77.             w: panel.boxObject.width,
  78.             x: panel.boxObject.x,
  79.             y: panel.boxObject.y
  80.         };
  81.         var w = win.w - panelbox.w - 160;
  82.  
  83.         var classes = [ 'resultstime', 'results_title', 'results_url', 'resultheader'];
  84.         var widths = [ w, w, w, w + 60];
  85.         var clen = classes.length;
  86.         for(var y = 0; y < clen; y++){
  87.             var a = document.getElementsByClassName(classes[y]);
  88.             var len = a.length;
  89.             var w = widths[y];
  90.             for(var x = 0; x < len; x++){
  91.                 a[x].setAttribute('style', 'width: ' + w + "px;");
  92.             }
  93.         }
  94.     },
  95.     queryTracker: function(){
  96.         var starttime = -1;
  97.         var endtime = -1;
  98.  
  99.         if(document.getElementById("searchfromcb").checked){
  100.             var d = document.getElementById("searchfrom").dateValue;
  101.             var d2 = new Date(
  102.                 d.getFullYear(),
  103.                 d.getMonth(),
  104.                 d.getDate(),
  105.                 0,
  106.                 0
  107.             );
  108.             starttime = d2.getTime();
  109.  
  110.         }
  111.         if(document.getElementById("searchtocb").checked){
  112.             var d = document.getElementById("searchto").dateValue;
  113.             var d2 = new Date(
  114.                 d.getFullYear(),
  115.                 d.getMonth(),
  116.                 d.getDate(),
  117.                 23,
  118.                 59,
  119.                 59
  120.             );
  121.             endtime = d2.getTime();
  122.         }
  123.         switch(bthistory.currResultType){
  124.             case 'visits':
  125.                 return bthistory.storage.queryTrackerByID(
  126.                     bthistory.currVisitID, 
  127.                     bthistory.currOrder, 
  128.                     bthistory.currOffset,
  129.                     bthistory.app.searchItemsPerPage,
  130.                     true,
  131.                     bthistory.currOrderDesc,
  132.                     bthistory.currTypes,
  133.                     starttime,
  134.                     endtime
  135.                 );
  136.             case 'more':
  137.                 return bthistory.storage.queryTrackerByMoreItems(
  138.                     bthistory.currMoreType, 
  139.                     bthistory._range.start,
  140.                     bthistory._range.end,
  141.                     bthistory.currOrder, 
  142.                     bthistory.currOffset,
  143.                     bthistory.app.searchItemsPerPage,
  144.                     true,
  145.                     bthistory.currOrderDesc
  146.                 );
  147.             case 'search':
  148.             default:
  149.                 return bthistory.storage.queryTrackerBySearch(
  150.                     bthistory.currQuery, 
  151.                     bthistory.currOrder, 
  152.                     bthistory.currOffset,
  153.                     bthistory.app.searchItemsPerPage,
  154.                     true,
  155.                     bthistory.currOrderDesc,
  156.                     bthistory.currTypes,
  157.                     starttime,
  158.                     endtime
  159.                 );
  160.         }
  161.     },
  162.     loadView: function(){
  163.         document.loadOverlay("chrome://boomtango/content/historyResults.xul", this);
  164.     },
  165.     observe: function(subject, topic, data) {
  166.         if(topic == "xul-overlay-merged"){
  167.             bthistory.app.log("historyResults::overlayLoaded");
  168.             this.loadMergedView();
  169.         }
  170.     },
  171.     /*
  172.         loadMergedView is handled after view has been merged.  
  173.     */
  174.     loadMergedView: function() {
  175.         bthistory.app.log("historyResults::loadMergedView");
  176.  
  177.         var data = bthistory._data.data;
  178.         this.total = bthistory._data.totalcount;
  179.         if(this.total){
  180.             this.loadHeader(data);
  181.             this.loadData(data);
  182.             this.loadPagination();
  183.         } else {
  184.             var body = document.getElementById("body");
  185.             var label= document.createElement("label");
  186.             label.setAttribute("value", bthistory.app.getString("history.nodatafound"));
  187.             label.className = "nodatafound";
  188.             body.appendChild(label);
  189.             document.getElementById("resultcontainer").setAttribute("hidden", "true");
  190.         }
  191.  
  192.         document.getElementById("duration_control").setAttribute("hidden", "true");
  193.         document.getElementById("gotoday").setAttribute("hidden", "true");
  194.         document.getElementById("gotodate").setAttribute("hidden", "true");
  195.         document.getElementById("filter_box").setAttribute("hidden", "true");
  196.         document.getElementById("searchdate_box").setAttribute("hidden", "false");
  197.         bthistory.resizeToWindow(true);
  198.     },
  199.     gotoPage: function(start){
  200.         bthistory.currOffset = start;
  201.         bthistory.resetHash();
  202.         bthistory.updateView();
  203.     },
  204.     loadPageClick: function(label, start){
  205.         var self = this;
  206.         label.addEventListener(
  207.             "click",
  208.             function(){
  209.                 self.gotoPage(start);
  210.             },
  211.             false
  212.         );
  213.     },
  214.     loadPagination: function(){
  215.         var container =  document.getElementById('resultcontainer');
  216.         var hbox = document.createElement("hbox");
  217.         var spacer = document.createElement('spacer');
  218.         spacer.setAttribute("flex", "1");
  219.         hbox.appendChild(spacer);
  220.  
  221.         const ITEMSPERPAGE = bthistory.app.searchItemsPerPage;
  222.  
  223.         if(bthistory.currOffset){
  224.             var vbox = document.createElement("vbox");
  225.             var spacer = document.createElement('spacer');
  226.             spacer.setAttribute("flex", "1");
  227.             vbox.appendChild(spacer);
  228.             var label= document.createElement("image");
  229.             label.id = "searchprev";
  230.             label.className = "resultsarrow";
  231.             label.setAttribute('src', 'chrome://boomtango/skin/arrow_left.png');
  232.             var start = bthistory.currOffset - ITEMSPERPAGE;
  233.             if(start < 0) {
  234.                 start = 0;
  235.             }
  236.             this.loadPageClick(label, start);
  237.             vbox.appendChild(label);
  238.             hbox.appendChild(vbox);
  239.         }
  240.  
  241.         var startIndex = Math.floor(bthistory.currOffset / (ITEMSPERPAGE * 10)) * 10;
  242.         var currIndex = Math.floor(bthistory.currOffset / ITEMSPERPAGE);
  243.         var endIndex = startIndex + 10;
  244.         if(this.total < endIndex * ITEMSPERPAGE){
  245.             endIndex = Math.floor(this.total / ITEMSPERPAGE) + ((this.total % ITEMSPERPAGE) > 0 ? 1 : 0);
  246.         }
  247.  
  248.         for(var x = startIndex; x < endIndex; x++){
  249.             if(x != currIndex){
  250.                 var label= document.createElement("label");
  251.                 var start = x * ITEMSPERPAGE;
  252.                 this.loadPageClick(label, start);
  253.                 label.className = "resultspagelink";
  254.                 label.setAttribute("value", (x + 1).toString());
  255.                 hbox.appendChild(label);
  256.             } else {
  257.                 var label= document.createElement("label");
  258.                 var start = x * ITEMSPERPAGE;
  259.                 label.className = "resultscurrpage";
  260.                 label.setAttribute("value", (x + 1).toString());
  261.                 hbox.appendChild(label);
  262.             }
  263.         }
  264.  
  265.         if(bthistory.currOffset + ITEMSPERPAGE < this.total){
  266.             var vbox = document.createElement("vbox");
  267.             var spacer = document.createElement('spacer');
  268.             spacer.setAttribute("flex", "1");
  269.             vbox.appendChild(spacer);
  270.             var label= document.createElement("image");
  271.             label.className = "resultsarrow";
  272.             label.setAttribute('src', 'chrome://boomtango/skin/arrow_right.png');
  273.             label.id = "searchnext";
  274.             var start = bthistory.currOffset + ITEMSPERPAGE;
  275.             this.loadPageClick(label, start);
  276.             vbox.appendChild(label);
  277.             hbox.appendChild(vbox);
  278.         }
  279.  
  280.         var spacer = document.createElement('spacer');
  281.         spacer.setAttribute("flex", "1");
  282.         hbox.appendChild(spacer);
  283.         container.appendChild(hbox);
  284.     },
  285.     loadHeader: function(data){
  286.         var label = document.getElementById("resultcount");
  287.         label.setAttribute("value",
  288.             bthistory.app.getString(
  289.                 "results.count",
  290.                 (bthistory.currOffset + 1).toString(),
  291.                 (bthistory.currOffset + data.length).toString(),
  292.                 this.total.toString()
  293.             )
  294.         );
  295.  
  296.         var menu = document.getElementById("sortby");
  297.          
  298.         if(bthistory.currOrder != "starttime" || !bthistory.currOrderDesc){
  299.             if(bthistory.currOrderDesc){
  300.                 if(bthistory.currOrder == "title"){
  301.                     menu.selectedIndex = 3;
  302.                 } else if(bthistory.currOrder == "timespent"){
  303.                     menu.selectedIndex = 4;
  304.                 }
  305.             } else {
  306.                 if(bthistory.currOrder == "starttime"){
  307.                     menu.selectedIndex = 1;
  308.                 } else if(bthistory.currOrder == "title"){
  309.                     menu.selectedIndex = 2;
  310.                 }
  311.             }
  312.         }
  313.         var self = this;
  314.         menu.addEventListener(
  315.             "select",
  316.             function(){
  317.                 self.handleSort();
  318.             },
  319.             false
  320.         );
  321.        document.getElementById("body_header").
  322.            setAttribute("hidden", "false");
  323.  
  324.     },
  325.     handleSort: function(){
  326.         bthistory.app.log("results::handleSort");
  327.         var menu = document.getElementById("sortby");
  328.         switch(menu.selectedIndex){
  329.             case 0:
  330.                 bthistory.currOrder = "starttime";
  331.                 bthistory.currOrderDesc = true;
  332.             break;
  333.             case 1:
  334.                 bthistory.currOrder = "starttime";
  335.                 bthistory.currOrderDesc = false;
  336.             break;
  337.             case 2:
  338.                 bthistory.currOrder = "title";
  339.                 bthistory.currOrderDesc = false;
  340.             break;
  341.             case 3:
  342.                 bthistory.currOrder = "title";
  343.                 bthistory.currOrderDesc = true;
  344.             break;
  345.             case 4:
  346.                 bthistory.currOrder = "timespent";
  347.                 bthistory.currOrderDesc = true;
  348.             break;
  349.         }
  350.  
  351.         bthistory.resetHash();
  352.         bthistory.updateView();
  353.     },
  354.     addStartClickHandler: function(linkNode, item){
  355.         linkNode.addEventListener(
  356.             "click",
  357.             function(){
  358.                 bthistory.currTime = item.starttime;
  359.                 bthistory.loadView('calendar', 'hour', item.ftsrowid);
  360.             },
  361.             false
  362.         );
  363.     },
  364.     loadData: function(data){
  365.         var len = data.length;
  366.         var fi = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
  367.         var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService2);
  368.         var container =  document.getElementById('resultsdata');
  369.         var ismoreitems = bthistory.currResultType == 'more';
  370.         for(var x = 0; x < len; x++){
  371.             var item = data[x];
  372.             var row = document.createElement('vbox');
  373.             row.className = "resultsbox"; 
  374.             row.setAttribute("contentID", item.ftsrowid);
  375.             if(ismoreitems){
  376.                 row.setAttribute("trackerID", item.id);
  377.             }
  378.  
  379.             var hcontainer = document.createElement('hbox');
  380.             var thumbnail = document.createElement('image');
  381.             thumbnail.className = "resultsthumb";
  382.             var thumbrec = boomtangoApp.getThumbID(item.url);
  383.             thumbnail.setAttribute("src",
  384.                 bthistory.app.getThumb(thumbrec.id));
  385.             hcontainer.appendChild(thumbnail);
  386.  
  387.             var vcontainer = document.createElement('vbox');
  388.  
  389.             // icon and title
  390.             var hbox = document.createElement('hbox');
  391.             hbox.setAttribute("flex", "1");
  392.             hbox.setAttribute("style", "background-color: transparent;");
  393.  
  394.                 
  395.             var title = item.title || item.url;
  396.             var vbox = document.createElement('vbox');
  397.             vbox.setAttribute("style", "background-color: transparent;");
  398.             var linkNode = document.createElement("label");
  399.             linkNode.className = "results_title";
  400.             linkNode.setAttribute("value", title);
  401.             linkNode.setAttribute("crop", "end");
  402.             linkNode.setAttribute("flex", "1");
  403.             hbox.appendChild(vbox);
  404.             hbox.appendChild(linkNode);
  405.             vcontainer.appendChild(hbox);
  406.  
  407.             //url
  408.             var hbox = document.createElement("hbox");
  409.             var linkNode = document.createElement("label");
  410.             linkNode.className = "text-link results_url";
  411.             linkNode.setAttribute("href",  item.url);
  412.             linkNode.setAttribute("style", 
  413.                     "background-color: transparent;color:blue;");
  414.             linkNode.setAttribute("value", item.url);
  415.             linkNode.setAttribute("crop", "end");
  416.             linkNode.setAttribute("flex", "1");
  417.             hbox.appendChild(linkNode);
  418.             var spacer = document.createElement("spacer");
  419.             spacer.setAttribute('flex', "1000");
  420.             hbox.appendChild(spacer);
  421.             vcontainer.appendChild(hbox);
  422.  
  423.             //starttime
  424.             var hbox = document.createElement("hbox");
  425.             hbox.className = "resultstime";
  426.             var label =document.createElement("label");
  427.             label.className = "resultslabel";
  428.             label.setAttribute('value',
  429.                 bthistory.app.getString("results.visited"));
  430.             hbox.appendChild(label);
  431.             var linkNode = document.createElement("label");
  432.             linkNode.className = "resultsstart";
  433.             linkNode.setAttribute("value", bthistory.datestring(item.starttime));
  434.             this.addStartClickHandler(linkNode, item);
  435.             linkNode.setAttribute("crop", "end");
  436.             linkNode.setAttribute("flex", "1");
  437.             hbox.appendChild(linkNode);
  438.  
  439.             //timespent
  440.             var label =document.createElement("label");
  441.             label.className = "resultslabel";
  442.             label.setAttribute('value',
  443.                 bthistory.app.getString("results.timespent"));
  444.             hbox.appendChild(label);
  445.             var linkNode = document.createElement("label");
  446.             linkNode.setAttribute("value", bthistory.duration(item.endtime - item.starttime));
  447.             linkNode.setAttribute("crop", "end");
  448.             linkNode.setAttribute("flex", "1");
  449.             hbox.appendChild(linkNode);
  450.  
  451.             var spacer = document.createElement("spacer");
  452.             spacer.setAttribute('flex', "1000");
  453.             hbox.appendChild(spacer);
  454.             vcontainer.appendChild(hbox);
  455.  
  456.             hcontainer.appendChild(vcontainer);
  457.             row.appendChild(hcontainer);
  458.             container.appendChild(row);
  459.  
  460.         }
  461.  
  462.         /*
  463.         if(len < bthistory.app.searchItemsPerPage){
  464.             var spacer = document.createElement('spacer');
  465.             spacer.setAttribute("flex", "1000");
  466.             container.appendChild(spacer);
  467.  
  468.         }
  469.         */
  470.     }
  471. };
  472.  
  473.